# main.py 主程序

import tkinter as tk
import tkinter.messagebox
import sys

from my_tkinter import MyApp
import setting
import path_history
from stdout_redirector import StdoutRedirector
import core

class App(MyApp):
    def __init__(self):
        root = tk.Tk()
        setting.Target_setting()
        path_history.PathHistory()
        
        super().__init__(root,path_history.PathHistory().source_paths,path_history.PathHistory().output_paths)

        MyApp.options3 = setting.Target_setting.file_extension
        self.entry1_set['values'] = MyApp.options3

        self.text_set.insert(tk.END,"\n".join(self.file_extensions)) # 在page2加载文件后缀

        root.mainloop()

    @property
    def file_extensions(self):
        MyApp.options3 = setting.Target_setting.file_extension
        self.entry1_set['values'] = MyApp.options3
        return setting.Target_setting.file_extension

    def process(self):
        # 前置准备
        target_source_path = self.entry1.get()
        target_output_path = self.entry2.get()
        print(target_source_path)
        print(target_output_path)

        sys.stdout = StdoutRedirector(self.text)

        print("执行记录")
        print("target_source_path",target_source_path)
        print("target_output_path",target_output_path)
        
        self.entry1.delete(0, tk.END)
        self.entry2.delete(0, tk.END)
        self.button.config(state=tk.DISABLED)


        core.coremain(target_source_path,target_output_path)

        path_history.PathHistory().add_source_path(target_source_path)
        path_history.PathHistory().add_output_path(target_output_path)
        self.entry1['values'] = path_history.PathHistory().source_paths
        self.entry2['values'] = path_history.PathHistory().output_paths

        self.text.insert(tk.END,"\n执行完成")
        self.button.config(state=tk.NORMAL)





    # 核心处理
    def setadd(self):
        get = self.entry1_set.get()
        re = setting.setting_add_file_extension(get)
        if re == 1:
            tkinter.messagebox.showerror("错误", "请勿输入空字符串")
            return 1
        elif re == 2:
            tkinter.messagebox.showerror("错误", "该文件后缀已存在")
            return 2
        self.entry1_set.delete(0, tk.END)
        self.text_set.delete("1.0", tk.END)
        # print(self.file_extensions)
        # print(setting.Target_setting.file_extension)
        self.text_set.insert(tk.END, "\n".join(self.file_extensions))

    def setremove(self):
        get = self.entry1_set.get()
        re = setting.setting_remove_file_extension(get)
        if re == 1:
            tkinter.messagebox.showerror("错误", "请勿输入空字符串")
            return 1
        elif re == 2:
            tkinter.messagebox.showerror("错误", "该文件后缀不存在")
            return 2

        self.entry1_set.delete(0, tk.END)
        self.text_set.delete("1.0", tk.END)
        self.text_set.insert(tk.END, "\n".join(self.file_extensions))
        

if __name__ == "__main__":
    app = App()